<template>
  <div style="height: 100%;background-color:white ;">
    <InsertPlatform ref="insertPlatform"/>
    <el-row>
      <el-col :span="12">
        <el-button size="small" type="success" @click="$refs.insertPlatform.visible = true">新建平台</el-button>
      </el-col>
      <el-col :span="8" style="right: auto">
      </el-col>
    </el-row>
    <el-row>
      <div>
        <el-table :data="platformTable.slice((currentPage-1)*pageSize,currentPage*pageSize)" :height="tableHeight"
                  border highlight-current-row stripe style="width: 100%;margin-top: 5px">
          <el-table-column label="平台名称" prop="name"></el-table-column>
          <el-table-column label="平台ID" prop="platformId"></el-table-column>
          <el-table-column label="绑定知识库" prop="knowledgeLibNames"></el-table-column>
          <el-table-column label="启用状态" width="">
            <template slot-scope="scope">
              <el-switch
                v-model="scope.row.status"
                @change="updatePlatform(scope.row)"/>
            </template>
          </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <span><el-link type="primary"
                             @click.prevent="goPlatformKnowledgeLib(scope.row.id,scope.row.name, scope.row.status)">编辑</el-link></span>
              <span><el-link type="danger" @click.prevent="deletePlatform(scope.row.id)">删除</el-link></span>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </el-row>
    <el-row>
      <div>
        <el-pagination :total="platformTable.length" :page-size="pageSize"
                       :current-page.sync="currentPage"
                       background
                       layout="total, prev, pager, next, jumper"
                       style="text-align: right;"></el-pagination>
      </div>
    </el-row>
  </div>
</template>

<script>
import InsertPlatform from "./insertPlatform";

export default {
  name: "platformIndex",
  components: {
    InsertPlatform,
  },
  data() {
    return {
      tableHeight: '',
      platformTable: [],
      uploadVisible: false,
      currentPage: 1, // 第几页
      pageSize: 10
    }
  },
  methods: {
    goPlatformKnowledgeLib(id, name, status) {
      this.$router.push({name: 'platformKnowledgeLib', params: {id, name, status}})
    },
    updatePlatform(platform) {
      let param = new URLSearchParams();
      param.append("platform", JSON.stringify(platform))
      this.axios.post('/pmph/action/platform/updatePlatform', param)
        .then(() => {
        }).catch((error) => {
        this.platformTable.forEach((item) => {
          //执行代码
          if (item.id == platform.id) {
            item.status = !item.status
          }
        })
      })
    },
    deletePlatform: function (id) {
      this.$confirm('是否删除？', '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let param = new URLSearchParams()
        param.append("id", id)
        this.axios.post('/pmph/action/platform/deletePlatform', param)
          .then((response) => {
            this.getPlatforms()
          })
      })
    },
    getPlatforms: function () {
      this.axios.get('/pmph/action/platform/selectPlatforms').then((response) => {
        this.platformTable = response.data
      }).catch((error) => {
        this.platformTable = [];
        this.$message.error("未能请求到数据");
      })
    },
  },
  created() {
    this.tableHeight = CONST.WORKSPACE_HEIGHT - 80;
    this.getPlatforms()
  }
}
</script>

<style scoped>
</style>
